﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyThuoc.BUS;
using System.Globalization;
namespace QuanLyThuoc.PresentionLayer
{
    public partial class frmThuoc : Form
    {
        
        private AutoCompleteStringCollection auTenNCC = new AutoCompleteStringCollection();
        public int keyValue = 0;
        public string keyValues = "";
        int vitri = 0;
        bool plAddMode = false;
        bool plEditMode = false;
        BindingSource dsthuoc;
        public static CultureInfo CulQD = new CultureInfo("");
        public frmThuoc()
        {
            InitializeComponent();
        }

        private void frmThuoc_Load(object sender, EventArgs e)
        {
            this.HienThiDanhSach();
            this.LoadNhaCC();
        }
        private void ResetControl()
        {
            this.txtIDThuoc .ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtTenThuoc.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtIDNCC.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtNCC.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtLoaiThuoc.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtNhomThuoc.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtDVTMua.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtDVT.ReadOnly = !(this.plAddMode || this.plEditMode);

            this.txtHeSoQD.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtDuongDung.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtCongDung.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtChongCD.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtBaoQuan.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtThoiHan.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtSoLuongMin.ReadOnly = !(this.plAddMode || this.plEditMode);


            this.cmdAdd.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.save24 : QuanLyThuoc.Properties.Resources.Add24;
            this.cmdEdit.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.undo24 : QuanLyThuoc.Properties.Resources.Edit24;

            this.cmdAdd.Text = (this.plAddMode || this.plEditMode) ? "Lưu [F2]" : "Thêm [F2]";
            this.cmdEdit.Text = (this.plAddMode || this.plEditMode) ? "Hủy [F4]" : "Sửa [F4]";

            this.grdList.Enabled = !this.plAddMode;

        }
        private void HienThiDanhSach()
        {
            dsthuoc = new BindingSource();
            dsthuoc.DataSource = ThuocBUS.SelectAll_Thuoc();
            grdList.AutoGenerateColumns = false;
            grdList.DataSource = dsthuoc;
        }
        private void LoadNhaCC()
        {
            this.auTenNCC.Clear();
            DataTable dtNCC = NhaCCBUS.LayMaNCCTheoTen();
            string lcValue = "";
            foreach (DataRow row in dtNCC.Rows)
            {

                lcValue = row["TenNCC"].ToString().PadRight(100, ' ') + "|" + row["IDNCC"].ToString().Trim();
                
                this.auTenNCC.Add(lcValue);
            }
            this.txtNCC.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.txtNCC.AutoCompleteMode = AutoCompleteMode.Suggest;
            this.txtNCC.AutoCompleteCustomSource = this.auTenNCC;
        }
        private void GanDK(DataGridViewRow r)
        {
            try
            {
                if (grdList.Rows.Count != 0)
                {

                    this.txtIDThuoc.Text = r.Cells[0].Value.ToString();
                    this.txtTenThuoc.Text = r.Cells[1].Value.ToString();
                    this.txtIDNCC.Text = r.Cells[2].Value.ToString();
                    this.txtNCC.Text = r.Cells[3].Value.ToString();
                    this.txtLoaiThuoc.Text = r.Cells[4].Value.ToString();
                    this.txtNhomThuoc.Text = r.Cells[5].Value.ToString();
                    this.txtDVTMua.Text = r.Cells[6].Value.ToString();
                    this.txtDVT.Text = r.Cells[7].Value.ToString();
                    this.txtHeSoQD.Text = r.Cells[8].Value.ToString();

                    this.txtDuongDung.Text = r.Cells[9].Value.ToString();
                    this.txtCongDung.Text = r.Cells[10].Value.ToString();
                    this.txtChongCD.Text = r.Cells[11].Value.ToString();
                    this.txtBaoQuan.Text = r.Cells[12].Value.ToString();
                    this.txtThoiHan.Text = r.Cells[13].Value.ToString();
                    this.txtSoLuongMin.Text = r.Cells[14].Value.ToString();
                }
                else
                {
                    this.txtIDThuoc.Text = "";
                    this.txtTenThuoc.Text = "";
                    this.txtIDNCC.Text = "";
                    this.txtNCC.Text = "";
                    this.txtLoaiThuoc.Text = "";
                    this.txtNhomThuoc.Text = "";
                    this.txtDVTMua.Text = "";
                    this.txtDVT.Text = "";
                    this.txtHeSoQD.Text = "";

                    this.txtDuongDung.Text = "";
                    this.txtCongDung.Text = "";
                    this.txtChongCD.Text = "";
                    this.txtBaoQuan.Text = "";
                    this.txtThoiHan.Text = "";
                    this.txtSoLuongMin.Text = "";
                }
            }
            catch (Exception ex)
            {

            }
        }
        public void Gan_Click()
        {
            try
            {
                if (this.grdList.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }
        private void AddNew()
        {
            this.plAddMode = true;
            this.plEditMode = false;


        }
        private void Edit()
        {
            this.plEditMode = true;
            this.plAddMode = false;
        }
        private void Undo()
        {
            //this.bmbList.CancelCurrentEdit();
            vitri = grdList.CurrentCell.RowIndex;
            this.plAddMode = false;
            this.plEditMode = false;
        }
        private void Save()
        {
           
         
           string tenthuoc= txtTenThuoc.Text ;
           int manhacc=int.Parse(txtIDNCC.Text) ;
           string tenncc= txtNCC.Text ;
           string loaithuoc= txtLoaiThuoc.Text;
           string nhomthuoc= txtNhomThuoc.Text ;
           string dvtmua= txtDVTMua.Text ;
           string dvt= txtDVT.Text ;
           decimal hosoQD=Convert.ToDecimal(txtHeSoQD.Text);

           string duongdung= txtDuongDung.Text ;
           string congdung= txtCongDung.Text ;
           string chongCD= txtChongCD.Text ;
           string dkbq= txtBaoQuan.Text ;
           int thoihansd=int.Parse(txtThoiHan.Text) ;
           int soluong=int.Parse(txtSoLuongMin.Text) ;

            if (string.IsNullOrEmpty(this.txtIDThuoc.Text.Trim()))
            {
                ThuocBUS.InsertThuoc(tenthuoc,dvt,dvtmua,hosoQD,loaithuoc,nhomthuoc,duongdung,congdung,chongCD,dkbq,manhacc,thoihansd,soluong);
                
            }
            else
            {
                int mathuoc=int.Parse(txtIDThuoc.Text) ;

                ThuocBUS.UpdateThuoc(mathuoc, tenthuoc, dvt, dvtmua, hosoQD, loaithuoc, nhomthuoc, duongdung, congdung, chongCD, dkbq, manhacc, thoihansd, soluong);
            }

        }
        private void Delete()
        {
            try
            {
                int mathuoc = int.Parse(txtIDThuoc.Text.ToString());

                if (MessageBox.Show("Bạn có muốn xóa thuốc '" + txtTenThuoc.Text + "'không ??", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (vitri == grdList.RowCount)
                    {
                        ThuocBUS.DeleteThuoc(mathuoc);
                        this.AddBlank();
                        grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                        grdList.Rows[vitri - 1].Selected = true;
                        Gan_Click();
                    }
                    if (vitri == 0)
                    {
                        ThuocBUS.DeleteThuoc(mathuoc);
                        Gan_Click();
                        grdList.CurrentCell = grdList.Rows[vitri + 1].Cells[0];
                        grdList.Rows[vitri + 1].Selected = true;
                        this.AddBlank();
                        Gan_Click();
                    }
                    if (vitri > 0 && vitri < grdList.RowCount)
                    {
                        ThuocBUS.DeleteThuoc(mathuoc);
                        this.AddBlank();
                        grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                        grdList.Rows[vitri - 1].Selected = true;
                        Gan_Click();
                    }

                }
            }
            catch (Exception ex)
            {
                // this.AddBlank();
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }
        private void Find()
        {
            if (txtFind.Text.Trim() == "")
            {
                this.HienThiDanhSach();
            }
            else
            {
                dsthuoc.DataSource = ThuocBUS.SearchThuocByCondition(txtFind.Text);
                grdList.DataSource = dsthuoc;
            }
        }
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData.Equals(Keys.Escape))
            {
                this.keyValues = "";
                if (!(this.plAddMode || this.plEditMode))
                {
                    this.Close();
                }
                else
                {
                    DialogResult dr = MessageBox.Show("Bạn có muốn đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                        this.Close();
                }
            }
            else if (keyData.Equals(Keys.F2))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Save();
                else
                    this.AddNew();
                
                this.ResetControl();
            }
            else if (keyData.Equals(Keys.F3))
            {
                if (!(this.plAddMode || this.plEditMode))
                {
                    if (string.IsNullOrEmpty(this.txtFind.Text.Trim()))
                    {
                        this.txtFind.Select();
                        this.txtFind.SelectAll();
                    }
                    else
                    {
                        this.Find();
                    }
                }
            }
            else if (keyData.Equals(Keys.F4))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Undo();
                else
                    this.Edit();
                this.ResetControl();
            }
           
            else if (keyData.Equals(Keys.F7))
            {
                if (!(this.plAddMode || this.plEditMode))
                    this.Delete();
                this.ResetControl();
            }

            return base.ProcessDialogKey(keyData);
        }
        private void grdList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (this.grdList.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }

        private void txtNCC_Leave(object sender, EventArgs e)
        {
            string[] arr = this.txtNCC.Text.Trim().Split('|');
            this.txtNCC.Text = arr[0].Trim();
            if (arr.Length > 1)
                this.txtIDNCC.Text = arr[1].Trim();
        }
        private void AddBlank()
        {
            this.txtIDThuoc.Text = "";
            this.txtTenThuoc.Text = "";
            this.txtIDNCC.Text = "";
            this.txtNCC.Text = "";
            this.txtLoaiThuoc.Text = "";
            this.txtNhomThuoc.Text = "";
            this.txtDVTMua.Text = "";
            this.txtDVT.Text = "";
            this.txtHeSoQD.Text = "";

            this.txtDuongDung.Text = "";
            this.txtCongDung.Text = "";
            this.txtChongCD.Text = "";
            this.txtBaoQuan.Text = "";
            this.txtThoiHan.Text = "";
            this.txtSoLuongMin.Text = "";
        }
        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (this.plAddMode || this.plEditMode)
                this.Save();
            else
                this.AddBlank();

            this.plAddMode = !this.plAddMode;
            this.HienThiDanhSach();
            this.ResetControl();
        }

        private void cmdEdit_Click(object sender, EventArgs e)
        {
            if (this.plAddMode || this.plEditMode)
            {
                this.Undo();
                this.HienThiDanhSach();
            }
            else
            {

                this.Edit();
            }
            this.HienThiDanhSach();
            this.ResetControl();
        }

        private void cmdDel_Click(object sender, EventArgs e)
        {
            this.Delete();
            HienThiDanhSach();
        }

        private void cmdPrev_Click(object sender, EventArgs e)
        {
            dsthuoc.MovePrevious();
        }

        private void cmdFirst_Click(object sender, EventArgs e)
        {
            dsthuoc.MoveFirst();
        }

        private void cmdNext_Click(object sender, EventArgs e)
        {
            dsthuoc.MoveNext();
        }

        private void cmdLast_Click(object sender, EventArgs e)
        {
            dsthuoc.MoveNext();
        }

        private void txtFind_KeyDown(object sender, KeyEventArgs e)
        {

            this.Find();
        }

        private void txtHeSoQD_TextChanged(object sender, EventArgs e)
        {
            decimal HS = int.Parse(this.txtHeSoQD.Text.Trim().Replace(".", "").PadLeft(1, '0'));
            this.txtHeSoQD.Text = HS.ToString("n0", CulQD);
        }
    }
}